package api

import (
	"gitee.com/golang-lib/go-framework/utils"
	"gitee.com/golang-lib/go-framework/web/base"
	"gitee.com/golang-lib/go-framework/web/ginx"
	"github.com/gin-gonic/gin"
	"net/http"
	"sys-admin/app/dao"
	"sys-admin/app/service"
	"time"
)

var RoleCtl = new(roleCtl)

type roleCtl struct{}

func (s *roleCtl) List(c *gin.Context) {
	page := ginx.QueryInt(c, "page", 1)
	size := ginx.QueryInt(c, "size", 10)
	list, count := service.RoleService.List(page, size)

	result := base.Result{Code: 0, Data: list}

	pages := utils.CalculatePage(int(count), size)
	pageInfo := base.PageInfo{Page: page, Size: size, Pages: pages, Total: int(count)}

	pageResult := base.PageResult{
		Result:   result,
		PageInfo: pageInfo,
	}

	c.JSON(http.StatusOK, pageResult)
}

func (s *roleCtl) Add(c *gin.Context) {
	var role dao.Role
	ginx.BindJsonAndValid(c, &role)

	now := time.Now()
	role.CreateTime = now
	role.UpdateTime = now

	err := role.Add()

	//result := dao.DB.Table("role").Create(role)
	//role = dao.SelectById(1)

	if err != nil {
		c.JSON(http.StatusOK, err.Error())
	} else {
		c.JSON(http.StatusOK, role)
	}

	//if result.Error != nil {
	//	c.JSON(http.StatusOK, base.Result{Code: -1, Message: result.Error.Error()})
	//} else {
	//	c.JSON(http.StatusOK, base.Result{Code: 0, Data: role.ID})
	//}
}

func (s *roleCtl) Update(c *gin.Context) {

}

func (s *roleCtl) Delete(c *gin.Context) {

}
